﻿#include "GLLInc.h"
#include "GLLConsole.h"
#include "GLLOSApp.h"

#ifdef GLL_WIN

namespace GLL
{
	Console::Console(){_TRACE = 0;}

	void Console::Create()
	{
		INIT();
		AllocConsole();
		_console = GetStdHandle(STD_OUTPUT_HANDLE);
	}

	void Console::Destory()
	{
		FreeConsole();
	}

	void Console::Print( const char* type, const char* str )
	{
		SEND(type, str);
		if (!_console) return;
		DWORD dwLen = 0;
		WriteConsole(_console, str, strlen(str), &dwLen, NULL);
	}

	//外部调试输出器

	void Console::INIT()
	{
		if(_TRACE) return;

		_TRACE = ::FindWindowA(0, "DBGOuput");
		SEND("_CMD_", "Reset");
	}

	void Console::SEND( const char* dbgtype, const char* str )
	{
		if(!_TRACE) return;
		std::stringstream DataStream;
		DataStream<<dbgtype<<"|"<<str;
		SEND_DATA(DataStream);
	}

	void Console::SEND_DATA( std::stringstream& DataStrm )
	{
		std::string strData = DataStrm.str();
		COPYDATASTRUCT data;
		data.cbData = strData.length();
		data.lpData = (PVOID)strData.c_str();
		OSApp& osaw = (OSApp&)OSApp::Ref();
		HWND hwnd = osaw.GetHWND();
		SendMessage(_TRACE, WM_COPYDATA, (WPARAM)hwnd, (LPARAM)&data);
	}

};//GLL



#endif

